#include <SimulationCore/GCC.h>
#include <SimulationCore/GDC.h>
#include <SimulationCore/Defines.h>
namespace SimulationCore
{
    GDC GCC::toGDC(bool isEllipsoid) const
    {
        double e = isEllipsoid ? GDC2GCC_E2 : GDC2GCC_E1;

        double S = sqrt(pow(m_x,2) + pow(m_y, 2));
        double L = atan2(m_y, m_x);

        double B = atan2(m_z, S * (1 - e));
        double delta;
        do {
            double sinB = sin(B);
            double N = EARTH_RADIUS / sqrt(1 - e * sinB * sinB);
            double newB = atan2(m_z + N * e * sinB, S);
            delta = abs(newB - B);
            B = newB;
        } while (delta > 1e-12);

        double cosB = cos(B);
        double N = EARTH_RADIUS / sqrt(1 - e * sin(B) * sin(B));
        double H = S / cosB - N;

        return  GDC(L * RAD_TO_DEG, B * RAD_TO_DEG, H);
    };
}